package com.enqualcomm.kids.view;

import android.text.InputFilter;
import android.text.Spanned;

import java.io.UnsupportedEncodingException;

/**
 * Created by Sunzhigang on 2016/4/21.
 */
public class FenceNameMyInputFilter implements InputFilter {
    @Override
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {


        try {
            int destLen = dest.toString().getBytes("GB18030").length;
            int sourceLen = source.toString().getBytes("GB18030").length;
            if (destLen + sourceLen > 8) {
                return "";
            }

            //如果按回退键
            if (source.length() < 1 && (dend - dstart >= 1)) {
                return dest.subSequence(dstart, dend - 1);
            }
            return source;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return source;
    }
}
